﻿@{
    ViewBag.Title = "权限控制";
}
@using ZhongKeSite.Entity
@model SysRight
@{
    ViewBag.Title = "权限控制";
    string JsonData = Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.TreeList);
}

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-header">@ViewBag.Title</div>
                <div class="layui-card-body">
                    <div id="roletree" class="demo-tree demo-tree-more"></div>
                </div>
            </div>
        </div>
        <div class="layui-col-md9" style="position: fixed;right:0;">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-tab">
                        <ul class="layui-tab-title">
                            <li class="layui-this">新增</li>
                            <li>编辑</li>
                            <li>删除</li>
                        </ul>
                        <div class="layui-tab-content">
                            <div class="layui-tab-item layui-show">
                                <form class="layui-form" action="@Url.Action("SysRightAdd","Sys",new { area = "Admin"})" lay-filter="rightadd-form">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">上级菜单</label>
                                        <div id="parentMenu" class="layui-input-block" style="font-weight:bold;height:36px;line-height:36px;vertical-align:middle;font-size:20px;">
                                            @(Model.RightName ?? "顶级菜单")
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">操作类型</label>
                                        <div class="layui-input-block">
                                            @Html.DropDownListFor(a => a.ActionType, (IEnumerable<SelectListItem>)ViewBag.SysDictCodeDLL)
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">权限ID</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="ID" lay-verify="required" maxlength="36" placeholder="请输入权限ID" autocomplete="on" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">权限名称</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="RightName" lay-verify="required" maxlength="36" placeholder="请输入权限名称" autocomplete="on" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">显示名称</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="DisplayName" lay-verify="required" maxlength="36" placeholder="请输入显示名称" autocomplete="on" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">链接地址</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="MenuUrl" lay-verify="required" maxlength="36" placeholder="请输入链接地址" autocomplete="on" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">菜单样式</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="SubSystem" maxlength="36" placeholder="请输入菜单样式" autocomplete="on" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">菜单图标</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="Pic" maxlength="50" placeholder="请输入菜单图标" autocomplete="on" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">显示顺序</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="SortNo" maxlength="6" placeholder="请输入显示顺序" autocomplete="on" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">类&nbsp;&nbsp;&nbsp;型</label>
                                        <div class="layui-input-block">
                                            <input type="radio" name="IsControl" value="1" title="按钮">
                                            <input type="radio" name="IsControl" value="2" title="列表">
                                            <input type="radio" name="IsControl" value="3" title="目录">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <input type="hidden" id="ParentCode" name="ParentCode" value="@Model.ID" />
                                            <button class="layui-btn" lay-submit lay-filter="addsubmit-form">保存</button>
                                            <a class="layui-btn layui-btn-primary" href="@Url.Action("SysRightList","Sys")">取消</a>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div class="layui-tab-item">
                                <form class="layui-form" action="@Url.Action("SysRightEdit","Sys",new { area = "Admin"})" lay-filter="rightedit-form">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">操作类型</label>
                                        <div class="layui-input-block">
                                            @Html.DropDownListFor(a => a.ActionType, (IEnumerable<SelectListItem>)ViewBag.SysDictCodeDLL)
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">权限名称</label>
                                        <div class="layui-input-block">
                                            @Html.TextBoxFor(a => a.RightName, new { @class = "layui-input", maxlength = 20 }) @Html.ValidationMessageFor(model => model.RightName)
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">显示名称</label>
                                        <div class="layui-input-block">
                                            @Html.TextBoxFor(a => a.DisplayName, new { @class = "layui-input", maxlength = 20 }) @Html.ValidationMessageFor(model => model.DisplayName)
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">链接地址</label>
                                        <div class="layui-input-block">
                                            @Html.TextBoxFor(a => a.MenuUrl, new { @class = "layui-input", maxlength = 200 })
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">菜单样式</label>
                                        <div class="layui-input-block">
                                            @Html.TextBoxFor(a => a.SubSystem, new { @class = "layui-input", maxlength = 50 })
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">菜单图标</label>
                                        <div class="layui-input-block">
                                            @Html.TextBoxFor(a => a.Pic, new { @class = "layui-input", maxlength = 50 })
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">显示顺序</label>
                                        <div class="layui-input-block">
                                            @Html.TextBoxFor(a => a.SortNo, new { @class = "layui-input" }) @Html.ValidationMessageFor(model => model.SortNo)
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">类&nbsp;&nbsp;&nbsp;型</label>
                                        <div class="layui-input-block">
                                            <input type="radio" name="IsControl" value="1" title="按钮" @(Model.IsControl == 1 ? "checked" : "")>
                                            <input type="radio" name="IsControl" value="2" title="列表" @(Model.IsTableControl == 1 ? "checked" : "")>
                                            <input type="radio" name="IsControl" value="3" title="目录" @(Model.IsMenu == 1 ? "checked" : "")>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <input type="hidden" name="ID" value="@Model.ID" />
                                            <button class="layui-btn" lay-submit lay-filter="rightsubmit-form">保存</button>
                                            <a class="layui-btn layui-btn-primary" href="@Url.Action("SysRightList","Sys",new { area = "Admin"})">取消</a>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div class="layui-tab-item">
                                <form class="layui-form" action="@Url.Action("SysRightDel","Sys",new { area = "Admin"})" lay-filter="rightdel-form">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">当前菜单</label>
                                        <div id="currentMenu" class="layui-input-block" style="font-weight:bold;height:36px;line-height:36px;vertical-align:middle;font-size:20px;">
                                            @(Model.RightName ?? "未选择")
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <input type="hidden" name="ID" value="@Model.ID" />
                                            <button class="layui-btn" lay-submit lay-filter="delsubmit-form">删除</button>
                                            <a class="layui-btn layui-btn-primary" href="@Url.Action("SysRightList","Sys",new { area = "Admin"})">取消</a>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                        </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['tree','form','element'], function () {
        var  layer = layui.layer
            , form = layui.form
            , tree = layui.tree
            //数据
            , data = window.eval('@Html.Raw(JsonData)');

        //基本演示
        tree.render({
            elem: '#roletree'
            , data: data
            ,id: 'treeId'
            , showLine: true
            , onlyIconControl: true
            ,isJump: true
            , click: function (obj) {
                var data = obj.data;  //获取当前点击的节点数据
                //window.location.href = '/Admin/Sys/SysRightList?id=' + data.id
                //layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
                 var loading;
                $.ajax({
                    url: "/Admin/Sys/SysRightList",
                    type: "POST",
                    data: {"id":data.id},
                    beforeSend: function () {
                        //注意，layer.msg默认3秒自动关闭，如果数据加载耗时比较长，需要设置time
                        loading = layer.msg('正在获取，请稍候……', { icon: 16, shade: 0.01, shadeClose: false, time: 10000 });
                    },
                    success: function (response) {
                        layer.close(loading);

                        if (response.success) {

                            var res = response.data;

                            form.val("rightadd-form", {
                                "ParentCode":res.ID
                            });
                            form.val("rightdel-form", {
                                "ID": res.ID
                            });

                            form.val("rightedit-form", {
                                "ActionType": res.ActionType,
                                "RightName": res.RightName,
                                "DisplayName": res.DisplayName,
                                "MenuUrl": res.MenuUrl,
                                "SubSystem": res.SubSystem,
                                "Pic": res.Pic,
                                "SortNo": res.SortNo,
                                "ID":res.ID,
                                "IsControl":res.IsControl == 1 ? 1 : (res.IsTableControl == 1 ? 2 : 3)
                            });

                            $("#parentMenu").html(!res.RightName ? "顶级菜单" : res.RightName);
                            $("#currentMenu").html(!res.RightName ? "顶级菜单" : res.RightName);

                        } else {
                            layer.msg(response.msg, { icon: 2, time: 1500 });
                        }
                    },
                    complete: function () {
                        layer.close(loading);
                    }
                });
            }
        });

        form.render(null, 'rightedit-form');
         /* 监听提交 */
        form.on('submit(rightsubmit-form)', function (data) {
              var right = data.field;
            if (right.IsControl == 3) {
                right.IsMenu = 1;
                right.IsTableControl = 0;
                right.IsControl = 0;
            }
            else if (right.IsControl == 2) {
                right.IsMenu = 0;
                right.IsTableControl = 1;
                right.IsControl = 0;
            }
            else {
                right.IsMenu = 0;
                right.IsTableControl = 0;
                right.IsControl = 1;
            }

            var loadingFlag;

            $.ajax({
                url: "/Admin/Sys/SysRightEdit",
                type: "POST",
                data: right,
                beforeSend: function () {
                    //注意，layer.msg默认3秒自动关闭，如果数据加载耗时比较长，需要设置time
                    loadingFlag = layer.msg('正在保存，请稍候……', { icon: 16, shade: 0.01, shadeClose: false, time: 10000 });
                },
                success: function (response) {
                    layer.close(loadingFlag);
                    if (response.success) {
                        //提交 Ajax 成功后，静态更新表格中的数据
                        layer.msg('保存成功', { icon: 1, time: 1500 }, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg(response.msg, { icon: 2, time: 1500 });
                    }
                },
                complete: function () {

                }
            });
            return false;
        });

        form.render(null, 'rightadd-form');
         /* 监听提交 */
        form.on('submit(addsubmit-form)', function (data) {
            var right = data.field;

            console.log(right);

            if (right.IsControl == 3) {
                right.IsMenu = 1;
                right.IsTableControl = 0;
                right.IsControl = 0;
            }
            else if (right.IsControl == 2) {
                right.IsMenu = 0;
                right.IsTableControl = 1;
                right.IsControl = 0;
            }
            else {
                right.IsMenu = 0;
                right.IsTableControl = 0;
                right.IsControl = 1;
            }

            var loadingFlag;

            $.ajax({
                url: "/Admin/Sys/SysRightAdd",
                type: "POST",
                data: right,
                beforeSend: function () {
                    //注意，layer.msg默认3秒自动关闭，如果数据加载耗时比较长，需要设置time
                    loadingFlag = layer.msg('正在保存，请稍候……', { icon: 16, shade: 0.01, shadeClose: false, time: 10000 });
                },
                success: function (response) {
                    layer.close(loadingFlag);
                    if (response.success) {
                        //提交 Ajax 成功后，静态更新表格中的数据
                        layer.msg('保存成功', { icon: 1, time: 1500 }, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg(response.msg, { icon: 2, time: 1500 });
                    }
                },
                complete: function () {

                }
            });
            return false;
        });

        form.render(null, 'rightdel-form');
        /* 监听提交 */
        form.on('submit(delsubmit-form)', function (data) {
            var right = data.field;
            console.log(right);

            //询问框
            layer.confirm('确认删除吗？', {
                btn: ['确定', '关闭'], //按钮
                title: '提示'
            }, function () {

                var loadingFlag;
                $.ajax({
                    url: "/Admin/Sys/SysRightDel",
                    type: "POST",
                    data: right,
                    beforeSend: function () {
                        //注意，layer.msg默认3秒自动关闭，如果数据加载耗时比较长，需要设置time
                        loadingFlag = layer.msg('正在保存，请稍候……', { icon: 16, shade: 0.01, shadeClose: false, time: 10000 });
                    },
                    success: function (response) {
                        layer.close(loadingFlag);
                        if (response.success) {
                            //提交 Ajax 成功后，静态更新表格中的数据
                            layer.msg('删除成功', { icon: 1, time: 1500 });
                            window.location.href = "/Sys/SysRightList";
                        } else {
                            layer.msg(response.msg, { icon: 2, time: 1500 });
                        }
                    },
                    complete: function () {

                    }
                });

            });

            return false;
        });

    });
</script>

